package com.shiro_02.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class MyConfig {

    //shiroFilerFactorBean   3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultSecurityManager")DefaultWebSecurityManager defaultSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultSecurityManager);

        //shrio添加过滤器：
        /*
        *   anon: 无需认证就可以访问
        *   authc: 必须认证了才能访问
        *   user: 必须拥有记住 我 功能才可以使用
        *   perms：拥有对某个资源的权限才能访问
        *   role： 拥有某个角色权限才可以访问
        * */
        HashMap<String, String> filtermap = new HashMap<>();
/*        filtermap.put("/user/add","anon");
        filtermap.put("/user/update","authc");*/
        // filtermap.put("/user/*","authc");
        filtermap.put("/user/add","perms[user:add]");
        filtermap.put("/user/update","perms[user:update]");
        bean.setFilterChainDefinitionMap(filtermap);
       //设置登录页面
        bean.setLoginUrl("/tologin");
        //未授权页面
        bean.setUnauthorizedUrl("/noatul");
        return bean;
    }

    //DafaulwebCutManner      2
    @Bean(name="defaultSecurityManager")
    public DefaultWebSecurityManager defaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        //关联Userrealm
        manager.setRealm(userRealm);
        return manager;
    }


    //创建 realm对象  1
    @Bean(name="userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }


     //整合shiroDialect: 用来 整合shiro thymenly

    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
